<?php

class MachinesController extends \BaseController {

	/**
	 * Display a listing of the resource.
	 *
	 * @return Response
	 */
	public function index()
	{
		//
        $pub = Pub::find(1);

        $PATH = $pub->path;
        if(Input::get('m_id')){
            $machine = Machine::find(Input::get('m_id'));
        }else{
            $machine = Machine::orderBy('created_at', 'DESC')->first();
        }




		require_once app_path().'/lib/wjs/jssdk.php';
		$jssdk = new JSSDK($pub->AppID,$pub->AppSecret);
		$signPackage = $jssdk->GetSignPackage();


        $machines = Machine::all();

        if(Input::get('area_id')){
//            $rates = Rate::all();
//            foreach ($rates as $rate) {
//                $xiaoqu = $rate->Service->mount->area_id;
//                $rate = Rate::find($rate->id);
//                $rate->xiaoqu = $xiaoqu;
//                $rate->save();
//            }
//            exit;

            $area = Area::find(Input::get('area_id'));
            $area_id = $area->id;
            $mounts = Mount::where('area_id',$area_id)->where('send_state',1)->orderBy('id', 'DESC')->get();
            $rates = Rate::where('xiaoqu',$area_id)->orderBy('id', 'DESC')->get();
//            $rates = Rate::where('machine_id',$machine->id)->orderBy('created_at', 'DESC')->get();
			if(Input::get('type') == 'test'){
				return View::make('machines.index_test2',compact('machines','machine','PATH','rates','area','mounts'));
			}else{
				return View::make('machines.index_test',compact('machines','machine','PATH','rates','area','mounts','signPackage'));
			}
        }else{
            $rates = Rate::where('machine_id',$machine->id)->orderBy('id', 'DESC')->get();
            return View::make('machines.index',compact('machines','machine','PATH','rates'));
        }
	}


	/**
	 * Show the form for creating a new resource.
	 *
	 * @return Response
	 */
	public function create()
	{
		//

	}


	/**
	 * Store a newly created resource in storage.
	 *
	 * @return Response
	 */
	public function store()
	{

	}


	/**
	 * Display the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function show($id)
	{
		//
//        var_dump($rates);
	}


	/**
	 * Show the form for editing the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function edit($id)
	{
		//
	}


	/**
	 * Update the specified resource in storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function update($id)
	{
		//
	}


	/**
	 * Remove the specified resource from storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($id)
	{
		//


	}



}
